package com.oig.sys.workflow.controller;

import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.util.SecurityUtil;
import com.oig.sys.workflow.app.IAppFlowConfigService;
import com.oig.sys.workflow.dto.FlowConfigDto;
import com.oig.sys.workflow.vo.FlowConfigVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Tag(name = "流程配置管理")
@RestController
@RequestMapping("/flowConfig")
@Slf4j
@RequiredArgsConstructor
public class TbFlowConfigController {

    private final IAppFlowConfigService appFlowConfigService;


    @Operation(summary = "查询三方审批流定义数据，配置审批流定义可以先去第三方获取到form单信息")
    @PostMapping(value = "/queryFlowDefine/{flowCode}")
    public FlowConfigVo queryFlowDefine(@PathVariable("flowCode") String flowCode){
        log.info(" query flow define by flow code:{}", flowCode);
        return this.appFlowConfigService.queryFlowDefine(flowCode);
    }


    //@PreAuthorize("@pms.hasPermission('/flowConfig/add')")
    //@SysLog(value="添加流程定义配置",apiUrl="/flowConfig/add")
    @Operation(summary = "添加流程定义配置")
    @PostMapping(value = "/add")
    public FlowConfigVo addFlowConfig(@RequestBody @Validated(GroupAdd.class) FlowConfigDto flowConfigDto){
        log.info("add flow config, {}", flowConfigDto);
        flowConfigDto.setCreateUserName(SecurityUtil.getDefineUserMain().getRealName());
        flowConfigDto.setCreateUserId(SecurityUtil.getDefineUserMain().getId());
        flowConfigDto.setCreateTime(new Date());
        flowConfigDto.setLastUpdateId(flowConfigDto.getCreateUserId());
        flowConfigDto.setLastUpdateName(flowConfigDto.getCreateUserName());
        flowConfigDto.setLastUpdateTime(flowConfigDto.getCreateTime());
        flowConfigDto.setTenantId(SecurityUtil.getDefineUserMain().getTenantId());
        flowConfigDto.setTenantName(SecurityUtil.getDefineUserMain().getTenantName());
        return this.appFlowConfigService.addFlowConfig(flowConfigDto);
    }

    //@PreAuthorize("@pms.hasPermission('/flowConfig/update')")
    //@SysLog(value="修改流程定义配置",apiUrl="/flowConfig/update")
    @Operation(summary = "修改流程定义配置")
    @PostMapping(value = "/update")
    public FlowConfigVo updateFlowConfig(@RequestBody @Validated(GroupEdit.class) FlowConfigDto flowConfigDto){
        log.info("update flow config, {}", flowConfigDto);
        flowConfigDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        flowConfigDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        flowConfigDto.setLastUpdateTime(new Date());
        return this.appFlowConfigService.updateFlowConfig(flowConfigDto);
    }


    @PreAuthorize("@pms.hasPermission('/flowConfig/cancel')")
    @SysLog(value="注销流程定义配置",apiUrl="/flowConfig/cancel")
    @Operation(summary = "删除流程定义配置")
    @PostMapping(value = "/cancel/{configId}")
    public SysResponse<Integer> cancelCronJob(@PathVariable("configId") Integer configId){
        log.info("cancel flow config id={}", configId) ;
//        this.checkRole(configId);
        this.appFlowConfigService.cancelFlowConfig(configId);
        return SysResponse.ok(configId,"删除流程定义配置成功");
    }

    @PreAuthorize("@pms.hasPermission('/flowConfig/subscribe')")
    @SysLog(value="开启订阅",apiUrl="/flowConfig/subscribe")
    @Operation(summary = "开启订阅")
    @PostMapping(value = "/subscribe/{configId}")
    public void subscribe(@PathVariable("configId") Integer configId){
        log.info(" subscribe config id=:{}", configId);
        this.appFlowConfigService.subscribe(configId);
    }

    @PreAuthorize("@pms.hasPermission('/flowConfig/unsubscribe')")
    @SysLog(value="关闭订阅",apiUrl="/flowConfig/unsubscribe")
    @Operation(summary = "关闭订阅")
    @PostMapping(value = "/unsubscribe/{configId}")
    public void unsubscribe(@PathVariable("configId") Integer configId){
        log.info(" unsubscribe config id=:{}", configId);
        this.appFlowConfigService.unsubscribe(configId);
    }


    @Operation(summary = "查看流程定义配置详细信息")
    @PostMapping(value = "/info/{configId}")
    public FlowConfigVo getFlowConfig(@PathVariable("configId") Integer configId){
        log.info(" get flow config id=:{}", configId);
        return this.appFlowConfigService.getFlowConfig(configId);
    }

}
